package com.ailk.aee.pcc.dao;

import com.ailk.aee.pcc.mongo.Mongo;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author pengzhang
 */
@Component
public class UserPloyStateDao {

    @Autowired
    private Mongo userPloyStateMongo;

    public void updatePloyUserState(String userId, String ployCode, String userState) {
        BasicDBObject basicDBObject = new BasicDBObject(ployCode, userState);
        userPloyStateMongo.getContentCollection().update(new BasicDBObject("userId", userId), new BasicDBObject("$set", basicDBObject), true, false);
    }

    //需要记录cpe解锁时间
    public void updatePloyUserState(String userId, String ployCode,
                                    String userState, String updatetime) {
        BasicDBObject targetObj = new BasicDBObject().append(ployCode,
                userState).append("updatetime", updatetime);

        userPloyStateMongo.getContentCollection().update(
                new BasicDBObject("userId", userId),
                new BasicDBObject("$set", targetObj), true, false);
    }

    public DBObject queryPloyUserState(String userId, String ployCode) {
        BasicDBObject basicDBObject = new BasicDBObject(ployCode, 1);
        return userPloyStateMongo.getContentCollection().findOne(new BasicDBObject("userId", userId), basicDBObject);
    }

    public DBObject queryUserState(String userId) {
        return userPloyStateMongo.getContentCollection().findOne(new BasicDBObject("userId", userId));
    }

    public void deletePloyUserState(String userId, String ployCode) {
        userPloyStateMongo.getContentCollection().update(new BasicDBObject("userId", userId), new BasicDBObject("$unset",
                new BasicDBObject(ployCode, 1)), false, true);
    }


}
